package com.mengxiangnongfu.controller.system;

import com.mengxiangnongfu.aspectj.annotation.Log;
import com.mengxiangnongfu.aspectj.enums.BusinessType;
import com.mengxiangnongfu.config.utils.ShiroSecurityUtils;
import com.mengxiangnongfu.dto.DeptDto;
import com.mengxiangnongfu.service.DeptService;
import com.mengxiangnongfu.vo.AjaxResult;
import com.mengxiangnongfu.vo.DataGridView;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * 部门管理
 *
 * @author Yang
 * @version 1.0
 * @date 2020/10/25 16:48
 */
@RequestMapping("system/dept")
@RestController
@Api(value = "科室管理")
public class DeptController {

    @Resource
    private DeptService deptService;

    /**
     * 分页查询部门信息
     *
     * @param deptDto
     * @return
     */
    @GetMapping("loadDeptByPage")
    public AjaxResult loadDeptByPage(DeptDto deptDto) {
        DataGridView dataGridView = this.deptService.loadAllDeptByPage(deptDto);
        return AjaxResult.success("查询部门信息成功!", dataGridView, dataGridView.getTotalCount());
    }

    /**
     * 添加部门信息
     *
     * @param deptDto
     * @return
     */
    @PostMapping("insertDept")
    @Log(title = "添加部门信息", businessType = BusinessType.INSERT)
    public AjaxResult insertDept(@Validated DeptDto deptDto) {
        deptDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.deptService.insertDept(deptDto));
    }


    /**
     * 修改部门信息
     *
     * @param deptDto
     * @return
     */
    @PutMapping("modifyDept")
    @Log(title = "修改部门信息", businessType = BusinessType.UPDATE)
    public AjaxResult modifyDept(@Validated DeptDto deptDto) {
        deptDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.deptService.modifyDept(deptDto));
    }

    /**
     * 根据编号查询部门信息
     *
     * @param deptId
     * @return
     */
    @GetMapping("getDeptById/{deptId}")
    public AjaxResult getDeptById(@PathVariable @Validated @NotBlank(message = "部门(科室)编号不能为空") Long deptId) {
        return AjaxResult.success(this.deptService.loadDeptById(deptId));
    }


    /**
     * 删除部门信息
     *
     * @param deptIds
     * @return
     */
    @DeleteMapping("removeDept/{deptIds}")
    public AjaxResult getDeptById(@PathVariable @Validated @NotBlank(message = "需要删除的部门(科室)编号不能为空") Long[] deptIds) {
        return AjaxResult.toAjax(this.deptService.removeDeptByIds(deptIds));
    }

    /**
     * 查询全部部门信息
     *
     * @return
     */
    @GetMapping("loadAllDept")
    public AjaxResult loadAllDept() {
        return AjaxResult.success("查询部门信息成功!", this.deptService.loadAllDept());
    }


}
